---
title: "Untitled"
output: html_document
---

############################################
Title: 4_03_Table_3.rmd
Purpose: This code runs baseline forecasting regression in Table 3
Last updated: 18-04-2023

Input:
    - Baseline macro data Latest version: final_agg_macro_data_monthly.csv 

Output:
    - Table 3

##########################################



########################################## 
1. Setup
########################################## 

0. Setup *Set your file path here*
```{r, results="hide", include=FALSE}
#Clear user defined environment
rm(list=ls())
#Clear memory

#Load packages
require(stargazer); require(lubridate); library(sandwich); library(ggplot2); require(dplyr); require(tidyverse); library(readxl); library(sandwich); library(lmtest); library(psych)
require(quantmod); require(dynlm); require(AER); require(vars); require(forecast); require(stargazer); require(strucchange); require(xts); require(lubridate); require(fGarch); require(zoo); require(timeSeries); library(sandwich); require(car);library(fBasics); library(ggplot2); require(dplyr); require(tidyverse); library(readxl); library(sandwich); library(QuantPsyc); library(tseries); library(latticeExtra); library(corrplot); library(psych) ; library(stringr); library(readr); library(see)

#options
options(digits=5)
options(stringsAsFactors = FALSE)
options(scipen=999)

#Assign dplyr verbs  
select <- dplyr::select
rename <- dplyr::rename
mutate <- dplyr::mutate
filter <- dplyr::filter
arrange <- dplyr::arrange
distinct <- dplyr::distinct
group_by <- dplyr::group_by
summarise <- dplyr::summarise
list <- base::list

#Define directories
global <- ".../2024_RFS_Replication_TOSUBMIT"


```

1.1 Load macro data
```{r, results="hide", include=FALSE}

final_agg_macro_data <- read_csv(paste0(global, "/Data/final_agg_macro_data_monthly.csv", ""), 
      col_types = cols(...1 = col_skip(), date = col_date(format = "%Y-%m-%d")))

```

1.2 Set sample period to use
```{r, results="hide", include=FALSE}


#Select sample period
macrofin_m_2000_extd = subset(final_agg_macro_data, date > "1999-11-01" & date < "2023-04-01")  

#Exclude GFC
macrofin_m_2000_extd_excrisis = subset(macrofin_m_2000_extd, date < "2007-12-01" | date > "2009-03-01") 


```



########################################## 
2. Table 3 results
##########################################

2.1 OLS Reg: Panel A
```{r, include=FALSE, results='hide'}

mod_1 = lm(lip_3m_ahead ~ agg_elp_5_fd + termspr + ffr_r  + lip_1m_behind  , data = macrofin_m_2000_extd)
# NewwyWest standard errors
nw_mod_1 = NeweyWest(lm(lip_3m_ahead ~ agg_elp_5_fd + termspr + ffr_r  + lip_1m_behind  , data = macrofin_m_2000_extd), lag = 4, prewhite = F, adjust = T)
coeftest(mod_1, vcov = nw_mod_1 )
robust_mod_1 = coeftest(mod_1, vcov = nw_mod_1 )[,3]
coef_mod_1=lm.beta(mod_1)


mod_2 = lm(lip_3m_ahead ~ loan_spr_fd + med_bidask + termspr + ffr_r  + lip_1m_behind  , data = macrofin_m_2000_extd)
# NewwyWest standard errors
nw_mod_2 = NeweyWest(lm(lip_3m_ahead ~ loan_spr_fd + med_bidask + termspr + ffr_r  + lip_1m_behind  , data = macrofin_m_2000_extd), lag = 4, prewhite = F, adjust = T)
coeftest(mod_2, vcov = nw_mod_2 )
robust_mod_2 = coeftest(mod_2, vcov = nw_mod_2 )[,3]
coef_mod_2=lm.beta(mod_2)


mod_3 = lm(lip_3m_ahead ~ loan_spr_fd + SP500_ret + termspr + ffr_r + lip_1m_behind, data = macrofin_m_2000_extd)
summary(mod_3)
# NewwyWest standard errors
nw_mod_3 = NeweyWest(lm(lip_3m_ahead ~ loan_spr_fd + SP500_ret + termspr + ffr_r + lip_1m_behind , data = macrofin_m_2000_extd), lag = 4, prewhite = F, adjust = T)
coeftest(mod_3, vcov = nw_mod_3 )
robust_mod_3 = coeftest(mod_3, vcov = nw_mod_3 )[,3]
coef_mod_3=lm.beta(mod_3)


mod_4 = lm(lip_3m_ahead ~ loan_spr_fd +  mean_vix + termspr + ffr_r + lip_1m_behind, data = macrofin_m_2000_extd)
# NewwyWest standard errors
nw_mod_4 = NeweyWest(lm(lip_3m_ahead ~ loan_spr_fd +  mean_vix + termspr + ffr_r + lip_1m_behind, data = macrofin_m_2000_extd), lag = 4, prewhite = F, adjust = T)
coeftest(mod_4, vcov = nw_mod_4 )
robust_mod_4 = coeftest(mod_4, vcov = nw_mod_4 )[,3]
coef_mod_4=lm.beta(mod_4)


mod_5 = lm(lip_3m_ahead ~ loan_spr_fd +  termspr + ffr_r  + lip_1m_behind + agg_bond_spr_HY_fd , data = macrofin_m_2000_extd)
# NewwyWest standard errors
nw_mod_5 = NeweyWest(lm(lip_3m_ahead ~  loan_spr_fd +  termspr + ffr_r  + lip_1m_behind + agg_bond_spr_HY_fd , data = macrofin_m_2000_extd), lag = 4, prewhite = F, adjust = T)
coeftest(mod_5, vcov = nw_mod_5 )
robust_mod_5 = coeftest(mod_5, vcov = nw_mod_5)[,3]
coef_mod_5=lm.beta(mod_5)


mod_6 = lm(lip_3m_ahead ~   loan_spr_fd +  termspr + ffr_r  + lip_1m_behind + agg_bond_spr_A_fd + agg_bond_spr_BBB_fd +  agg_bond_spr_HY_fd , data = macrofin_m_2000_extd)
# NewwyWest standard errors
nw_mod_6 = NeweyWest(lm(lip_3m_ahead ~  loan_spr_fd +  termspr + ffr_r  + lip_1m_behind + agg_bond_spr_A_fd + agg_bond_spr_BBB_fd +  agg_bond_spr_HY_fd , data = macrofin_m_2000_extd), lag = 4, prewhite = F, adjust = T)
coeftest(mod_6, vcov = nw_mod_6 )
robust_mod_6 = coeftest(mod_6, vcov = nw_mod_6)[,3]
coef_mod_6=lm.beta(mod_6)


stargazer(mod_1, mod_2, mod_3, mod_4, mod_5, mod_6,  
          type = "text", 
          align = TRUE, 
          #no.space = TRUE, 
          dep.var.labels = c("Industrial Production - 3m ahead"), 
          se = list( robust_mod_1, robust_mod_2, robust_mod_3 , robust_mod_4, robust_mod_5, robust_mod_6), 
          coef = list(coef_mod_1, coef_mod_2, coef_mod_3, coef_mod_4, coef_mod_5, coef_mod_6),
          omit = c( "Constant", "lip_1m_behind","lempl_1m_behind", "uerate_1m_behind", "tcu_1m_behind", "lnew_1m_behind", "linv_1m_behind" )
                )



```

2.2 OLS Reg: Panel B
```{r, include=FALSE, results='hide'}


mod_lip = lm(lip_3m_ahead ~ loan_spr_fd    + termspr + ffr_r  + lip_1m_behind  , data = macrofin_m_2000_extd_excrisis)
# NewwyWest standard errors
nw_mod_lip = NeweyWest(lm(lip_3m_ahead ~  loan_spr_fd    + termspr + ffr_r  + lip_1m_behind , data = macrofin_m_2000_extd_excrisis), lag = 4, prewhite = F, adjust = T)
coeftest(mod_lip, vcov = nw_mod_lip )
robust_mod_lip = coeftest(mod_lip, vcov = nw_mod_lip)[,3]
coef_mod_lip=lm.beta(mod_lip)


mod_ism_man = lm(ism_man_3m_ahead ~ loan_spr_fd  +  termspr + ffr_r  + ism_man_1m_behind  , data = macrofin_m_2000_extd_excrisis)
# NewwyWest standard errors
nw_mod_ism_man = NeweyWest(lm(ism_man_3m_ahead ~ loan_spr_fd  +  termspr + ffr_r  + ism_man_1m_behind , data = macrofin_m_2000_extd_excrisis), lag = 4, prewhite = F, adjust = T)
coeftest(mod_ism_man, vcov = nw_mod_ism_man )
robust_mod_ism_man = coeftest(mod_ism_man, vcov = nw_mod_ism_man)[,3]
coef_mod_ism_man=lm.beta(mod_ism_man)



mod_ism_nonman = lm(ism_nonman_3m_ahead ~ loan_spr_fd +  termspr + ffr_r  + ism_nonman_1m_behind  , data = macrofin_m_2000_extd_excrisis)
# NewwyWest standard errors
nw_mod_ism_nonman = NeweyWest(lm(ism_nonman_3m_ahead ~ loan_spr_fd +  termspr + ffr_r  + ism_nonman_1m_behind , data = macrofin_m_2000_extd_excrisis), lag = 4, prewhite = F, adjust = T)
coeftest(mod_ism_nonman, vcov = nw_mod_ism_nonman )
robust_mod_ism_nonman = coeftest(mod_ism_nonman, vcov = nw_mod_ism_nonman)[,3]
coef_mod_ism_nonman=lm.beta(mod_ism_nonman)



mod_tcu = lm(tcu_3m_ahead ~  loan_spr_fd    + termspr + ffr_r  + tcu_1m_behind  , data = macrofin_m_2000_extd_excrisis)
# NewwyWest standard errors
nw_mod_tcu = NeweyWest(lm(tcu_3m_ahead ~  loan_spr_fd    + termspr + ffr_r  + tcu_1m_behind , data = macrofin_m_2000_extd_excrisis), lag = 4, prewhite = F, adjust = T)
coeftest(mod_tcu, vcov = nw_mod_tcu )
robust_mod_tcu = coeftest(mod_tcu, vcov = nw_mod_tcu)[,3]
coef_mod_tcu=lm.beta(mod_tcu)



mod_lnew = lm(lnew_3m_ahead ~  loan_spr_fd    + termspr + ffr_r  + lnew_1m_behind  , data = macrofin_m_2000_extd_excrisis)
# NewwyWest standard errors
nw_mod_lnew = NeweyWest(lm(lnew_3m_ahead ~  loan_spr_fd    + termspr + ffr_r  + lnew_1m_behind , data = macrofin_m_2000_extd_excrisis), lag = 4, prewhite = F, adjust = T)
coeftest(mod_lnew, vcov = nw_mod_lnew )
robust_mod_lnew = coeftest(mod_lnew, vcov = nw_mod_lnew)[,3]
coef_mod_lnew=lm.beta(mod_lnew)



mod_linv = lm(linv_3m_ahead ~  loan_spr_fd    + termspr + ffr_r  + linv_1m_behind  , data = macrofin_m_2000_extd_excrisis)
# NewwyWest standard errors
nw_mod_linv = NeweyWest(lm(linv_3m_ahead ~  loan_spr_fd    + termspr + ffr_r  + linv_1m_behind , data = macrofin_m_2000_extd_excrisis), lag = 4, prewhite = F, adjust = T)
coeftest(mod_linv, vcov = nw_mod_linv )
robust_mod_linv = coeftest(mod_linv, vcov = nw_mod_linv)[,3]
coef_mod_linv=lm.beta(mod_linv)



stargazer(mod_lip, mod_tcu, mod_lnew, mod_linv, mod_ism_man, mod_ism_nonman,
          type = "text", 
          align = TRUE, 
          #no.space = TRUE, 
          #dep.var.labels = c("coincident Index - 12m ahead"), 
            se = list(robust_mod_lip, robust_mod_tcu, robust_mod_lnew, robust_mod_linv, robust_mod_ism_man, robust_mod_ism_nonman), 
          coef = list(coef_mod_lip, coef_mod_tcu, coef_mod_lnew, coef_mod_linv, coef_mod_ism_man, coef_mod_ism_nonman),
          omit = c( "Constant", "lip_1m_behind","ism_man_1m_behind", "ism_nonman_1m_behind", "tcu_1m_behind", "lnew_1m_behind", "linv_1m_behind" )
          #report = "vct"
          )



```

2.3 OLS Reg: Panel C
```{r, include=FALSE, results='hide'}


mod_lip = lm(lip_3m_ahead ~ loan_spr_fd + first_pc_fd + med_bidask + SP500_ret + mean_vix   + termspr + ffr_r  + lip_1m_behind  , data = macrofin_m_2000_extd)
# NewwyWest standard errors
nw_mod_lip = NeweyWest(lm(lip_3m_ahead ~  loan_spr_fd + first_pc_fd + med_bidask + SP500_ret + mean_vix   + termspr + ffr_r  + lip_1m_behind , data = macrofin_m_2000_extd), lag = 4, prewhite = F, adjust = T)
coeftest(mod_lip, vcov = nw_mod_lip )
robust_mod_lip = coeftest(mod_lip, vcov = nw_mod_lip)[,3]
coef_mod_lip=lm.beta(mod_lip)



mod_ism_man = lm(ism_man_3m_ahead ~ loan_spr_fd + first_pc_fd + med_bidask + SP500_ret + mean_vix  +  termspr + ffr_r  + ism_man_1m_behind  , data = macrofin_m_2000_extd)
# NewwyWest standard errors
nw_mod_ism_man = NeweyWest(lm(ism_man_3m_ahead ~ loan_spr_fd + first_pc_fd + med_bidask + SP500_ret + mean_vix  +  termspr + ffr_r  + ism_man_1m_behind , data = macrofin_m_2000_extd), lag = 4, prewhite = F, adjust = T)
coeftest(mod_ism_man, vcov = nw_mod_ism_man )
robust_mod_ism_man = coeftest(mod_ism_man, vcov = nw_mod_ism_man)[,3]
coef_mod_ism_man=lm.beta(mod_ism_man)



mod_ism_nonman = lm(ism_nonman_3m_ahead ~ loan_spr_fd + first_pc_fd + med_bidask + SP500_ret + mean_vix  +  termspr + ffr_r  + ism_nonman_1m_behind  , data = macrofin_m_2000_extd)
# NewwyWest standard errors
nw_mod_ism_nonman = NeweyWest(lm(ism_nonman_3m_ahead ~ loan_spr_fd + first_pc_fd + med_bidask + SP500_ret + mean_vix +  termspr + ffr_r  + ism_nonman_1m_behind , data = macrofin_m_2000_extd), lag = 4, prewhite = F, adjust = T)
coeftest(mod_ism_nonman, vcov = nw_mod_ism_nonman )
robust_mod_ism_nonman = coeftest(mod_ism_nonman, vcov = nw_mod_ism_nonman)[,3]
coef_mod_ism_nonman=lm.beta(mod_ism_nonman)


mod_tcu = lm(tcu_3m_ahead ~  loan_spr_fd + first_pc_fd + med_bidask + SP500_ret + mean_vix   + termspr + ffr_r  + tcu_1m_behind  , data = macrofin_m_2000_extd)
# NewwyWest standard errors
nw_mod_tcu = NeweyWest(lm(tcu_3m_ahead ~  loan_spr_fd + first_pc_fd + med_bidask + SP500_ret + mean_vix   + termspr + ffr_r  + tcu_1m_behind , data = macrofin_m_2000_extd), lag = 4, prewhite = F, adjust = T)
coeftest(mod_tcu, vcov = nw_mod_tcu )
robust_mod_tcu = coeftest(mod_tcu, vcov = nw_mod_tcu)[,3]
coef_mod_tcu=lm.beta(mod_tcu)



mod_lnew = lm(lnew_3m_ahead ~  loan_spr_fd + first_pc_fd + med_bidask + SP500_ret + mean_vix   + termspr + ffr_r  + lnew_1m_behind  , data = macrofin_m_2000_extd)
# NewwyWest standard errors
nw_mod_lnew = NeweyWest(lm(lnew_3m_ahead ~  loan_spr_fd + first_pc_fd + med_bidask + SP500_ret + mean_vix   + termspr + ffr_r  + lnew_1m_behind , data = macrofin_m_2000_extd), lag = 4, prewhite = F, adjust = T)
coeftest(mod_lnew, vcov = nw_mod_lnew )
robust_mod_lnew = coeftest(mod_lnew, vcov = nw_mod_lnew)[,3]
coef_mod_lnew=lm.beta(mod_lnew)



mod_linv = lm(linv_3m_ahead ~  loan_spr_fd + first_pc_fd + med_bidask + SP500_ret + mean_vix   + termspr + ffr_r  + linv_1m_behind  , data = macrofin_m_2000_extd)
# NewwyWest standard errors
nw_mod_linv = NeweyWest(lm(linv_3m_ahead ~  loan_spr_fd + first_pc_fd + med_bidask + SP500_ret + mean_vix   + termspr + ffr_r  + linv_1m_behind , data = macrofin_m_2000_extd), lag = 4, prewhite = F, adjust = T)
coeftest(mod_linv, vcov = nw_mod_linv )
robust_mod_linv = coeftest(mod_linv, vcov = nw_mod_linv)[,3]
coef_mod_linv=lm.beta(mod_linv)


stargazer(mod_lip, mod_tcu, mod_lnew, mod_linv, mod_ism_man, mod_ism_nonman,
          type = "text", 
          align = TRUE, 
          #no.space = TRUE, 
          #dep.var.labels = c("coincident Index - 12m ahead"), 
            se = list(robust_mod_lip, robust_mod_tcu, robust_mod_lnew, robust_mod_linv, robust_mod_ism_man, robust_mod_ism_nonman), 
          coef = list(coef_mod_lip, coef_mod_tcu, coef_mod_lnew, coef_mod_linv, coef_mod_ism_man, coef_mod_ism_nonman),
          omit = c( "Constant", "lip_1m_behind","lempl_1m_behind", "uerate_1m_behind", "tcu_1m_behind", "lnew_1m_behind", "linv_1m_behind" )
          )

```



